package com.supreme.proxyserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.proxyserver.bean.RDSUser;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "redis")
public class RedisController {

    private static final Long redisExpire = 1L;
    private static final TimeUnit redisTimeUnit = TimeUnit.HOURS;

    @Resource
    private RedisTemplate<String, Serializable> redisTemplate;

//    @Resource(name = "redisTemplate3")
//    private RedisTemplate<String, Serializable> redisTemplate3;

//    @Resource(name = "redisTemplate4")
//    private RedisTemplate<String, Serializable> redisTemplate4;

    /*@Autowired
    public RedisController(RedisTemplate<String, Serializable> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }*/

    @GetMapping(value = "keys/{pattern}")
    public AjaxResp keys(@PathVariable(value = "pattern") String pattern) {
        final Set<String> keys = this.redisTemplate.keys(pattern);
        return new AjaxResp(keys);
    }

    @PostMapping(value = "insertUser")
    public AjaxResp insertUser(@RequestBody RDSUser user) {
        final ValueOperations<String, Serializable> operations = this.redisTemplate.opsForValue();
        operations.set(user.getUserId(), user, redisExpire, redisTimeUnit);
        return new AjaxResp();
    }

    @GetMapping(value = "selectUser/{dataKey}")
    public AjaxResp selectUser(@PathVariable(value = "dataKey") String dataKey) {
        final ValueOperations<String, Serializable> valueOperations = this.redisTemplate.opsForValue();
        final Serializable serializable = valueOperations.get(dataKey);
        return new AjaxResp(serializable);
    }

    /*@PostMapping(value = "insertUserDb3")
    public AjaxResp insertUserDb3(@RequestBody RDSUser user) {
        final ValueOperations<String, Serializable> operations = this.redisTemplate3.opsForValue();
        operations.set(user.getUserId(), user, redisExpire, redisTimeUnit);
        return new AjaxResp();
    }*/

    /*@GetMapping(value = "selectUserDb3/{dataKey}")
    public AjaxResp selectUserDb3(@PathVariable(value = "dataKey") String dataKey) {
        final ValueOperations<String, Serializable> valueOperations = this.redisTemplate3.opsForValue();
        final Serializable serializable = valueOperations.get(dataKey);
        return new AjaxResp(serializable);
    }*/

    @PostMapping(value = "insertUser2List/{dataKey}")
    public AjaxResp insertUser2List(@PathVariable(value = "dataKey") String dataKey,
                                    @RequestBody RDSUser user) {
        final ListOperations<String, Serializable> serializableListOperations = this.redisTemplate.opsForList();
        final Long leftPush = serializableListOperations.leftPush(dataKey, user);
        this.redisTemplate.expire(dataKey, redisExpire, redisTimeUnit);
        return new AjaxResp(leftPush);
    }

    @GetMapping(value = "selectUser2List/{dataKey}")
    public AjaxResp selectUser2List(@PathVariable(value = "dataKey") String dataKey) {
        List<Serializable> userList = this.redisTemplate.opsForList().range(dataKey, 0, 100);
        return new AjaxResp(userList);
    }

    @PostMapping(value = "insertUser2Set/{dataKey}")
    public AjaxResp insertUser2Set(@PathVariable(value = "dataKey") String dataKey,
                                   @RequestBody RDSUser user) {
        SetOperations<String, Serializable> serializableSetOperations = this.redisTemplate.opsForSet();
        Long result = serializableSetOperations.add(dataKey, user);
        this.redisTemplate.expire(dataKey, redisExpire, redisTimeUnit);
        return new AjaxResp(result);
    }

    @GetMapping(value = "selectUser2Set/{dataKey}")
    public AjaxResp selectUser2Set(@PathVariable(value = "dataKey") String dataKey) {
        SetOperations<String, Serializable> serializableSetOperations = this.redisTemplate.opsForSet();
        final Set<Serializable> members = serializableSetOperations.members(dataKey);
        return new AjaxResp(members);
    }

    @PostMapping(value = "insertUser2Hash/{dataKey}")
    public AjaxResp insertUser2Hash(@PathVariable(value = "dataKey") String dataKey,
                                    @RequestBody RDSUser user) {
        HashOperations<String, Object, Object> objectObjectHashOperations = this.redisTemplate.opsForHash();
        objectObjectHashOperations.put(dataKey, user.getUserId(), user);
        this.redisTemplate.expire(dataKey, redisExpire, redisTimeUnit);
        return new AjaxResp();
    }

    @GetMapping(value = "selectUser2Hash/{dataKey}")
    public AjaxResp selectUser2Hash(@PathVariable(value = "dataKey") String dataKey) {
        final HashOperations<String, Object, Object> objectObjectHashOperations = this.redisTemplate.opsForHash();
        final Map<Object, Object> entries = objectObjectHashOperations.entries(dataKey);
        return new AjaxResp(entries);
    }

    @PostMapping(value = "insertUser2ZSet/{dataKey}/{index}")
    public AjaxResp insertUser2ZSet(@PathVariable(value = "dataKey") String dataKey,
                                    @PathVariable(value = "index") Double index,
                                    @RequestBody RDSUser user) {
        final ZSetOperations<String, Serializable> serializableZSetOperations = this.redisTemplate.opsForZSet();
        final Boolean add = serializableZSetOperations.add(dataKey, user, index);
        this.redisTemplate.expire(dataKey, redisExpire, redisTimeUnit);
        return new AjaxResp(add);
    }

    @GetMapping(value = "selectUser2ZSet/{dataKey}")
    public AjaxResp selectUser2ZSet(@PathVariable(value = "dataKey") String dataKey) {
        final ZSetOperations<String, Serializable> serializableZSetOperations = this.redisTemplate.opsForZSet();
        final Set<Serializable> range = serializableZSetOperations.range(dataKey, 0, 100);
        return new AjaxResp(range);
    }
}
